package com.example.mapper;

import com.example.anno.AutoFill;
import com.example.enums.OperationType;
import com.example.pojo.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {

    /**
     * 新增分类
     *
     * @param category 分类对象
     */
    @Insert("insert into category(category_name, category_alias, create_user, create_time, update_time) " +
            "VALUES( #{categoryName}, #{categoryAlias}, #{createUser}, #{createTime}, #{updateTime})")
    @AutoFill(OperationType.INSERT)
    void add(final Category category);

    /**
     * 获取文章分类列表
     *
     * @param userId 用户id
     */
    @Select("select * from category where create_user = #{userId}")
    List<Category> list(final Integer userId);

    /**
     * 根据id查询分类详情
     *
     * @param id 分类id
     * @return 分类详情
     */
    @Select("select * from category where id = #{id}")
    Category findById(final Integer id);

    /**
     * 更新文章分类
     *
     * @param category 文章分类
     */
    @Update("update category " +
            "set category_name = #{categoryName}, " +
            "category_alias = #{categoryAlias}, " +
            "update_time = #{updateTime} " +
            "where id = #{id}")
    @AutoFill(OperationType.UPDATE)
    void update(final Category category);

    /**
     * 根据id删除分类详情
     *
     * @param id 分为id
     */
    @Delete("delete from category where id = #{id}")
    void delete(final Integer id);

}
